<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1"><title id="ef20941">SAVEPOINT</title><body><p id="sql_command_desc">Defines a new savepoint within the current transaction.</p><section id="section2"><title>Synopsis</title><codeblock id="sql_command_synopsis">SAVEPOINT <varname>savepoint_name</varname></codeblock></section><section id="section3"><title>Description</title><p><codeph>SAVEPOINT</codeph> establishes a new savepoint within the current
transaction. </p><p>A savepoint is a special mark inside a transaction that allows all
commands that are run after it was established to be rolled back,
restoring the transaction state to what it was at the time of the savepoint.</p></section><section id="section4"><title>Parameters</title><parml><plentry><pt><varname>savepoint_name</varname></pt><pd>The name of the new savepoint.</pd></plentry></parml></section><section id="section5"><title>Notes</title><p>Use <codeph><xref href="ROLLBACK_TO_SAVEPOINT.xml#topic1" type="topic" format="dita"/></codeph>
                to rollback to a savepoint. Use <codeph><xref href="RELEASE_SAVEPOINT.xml#topic1"
                        type="topic" format="dita"/></codeph> to destroy a savepoint, keeping the
                effects of commands run after it was established.</p><p>Savepoints can only be established when inside a transaction block.
There can be multiple savepoints defined within a transaction.</p></section><section id="section6"><title>Examples</title><p>To establish a savepoint and later undo the effects of all commands
run after it was established:</p><codeblock>BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (2);
    ROLLBACK TO SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (3);
COMMIT;</codeblock><p>The above transaction will insert the values 1 and 3, but not 2. </p><p>To establish and later destroy a savepoint:</p><codeblock>BEGIN;
    INSERT INTO table1 VALUES (3);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (4);
    RELEASE SAVEPOINT my_savepoint;
COMMIT;</codeblock><p>The above transaction will insert both 3 and 4. </p></section><section id="section7"><title>Compatibility</title><p>SQL requires a savepoint to be destroyed automatically when another savepoint with the same name
                is established. In Greenplum Database, the old savepoint is kept, though only the
                more recent one will be used when rolling back or releasing. (Releasing the newer
                savepoint will cause the older one to again become accessible to <codeph><xref
                        href="ROLLBACK_TO_SAVEPOINT.xml#topic1" type="topic" format="dita"
                    /></codeph> and <codeph><xref href="RELEASE_SAVEPOINT.xml#topic1" type="topic"
                        format="dita"/></codeph>.) Otherwise, <codeph>SAVEPOINT</codeph> is fully
                SQL conforming.</p></section><section id="section8"><title>See Also</title><p><codeph><xref href="BEGIN.xml#topic1" type="topic" format="dita"/></codeph>, <codeph><xref
                        href="COMMIT.xml#topic1" type="topic" format="dita"/></codeph>,
                        <codeph><xref href="ROLLBACK.xml#topic1" type="topic" format="dita"
                    /></codeph>, <codeph><xref href="RELEASE_SAVEPOINT.xml#topic1" type="topic"
                        format="dita"/></codeph>, <codeph><xref
                        href="ROLLBACK_TO_SAVEPOINT.xml#topic1" type="topic" format="dita"
                    /></codeph></p></section></body></topic>
